package blue.global.base;

import org.aspectj.lang.JoinPoint;
import org.aspectj.util.Reflection;

import blue.global.util.ReflectionUtil;

/**
 * 写日志
 * 
 * @author zhengj
 * @since 2010-1-17 1.0
 */
public class Logger
{
	public Logger()
	{
	}
	
	public void error(JoinPoint point, Exception e)
	{
		System.out.println("参数===========");
		System.out.println(this.getContent(point));
		System.out.println("异常===========");
		System.out.println(this.getStackTrace(e));
	}
	
	/**
	 * 取得异常栈信息
	 * 
	 * @param e 异常
	 * @return 异常信息
	 */
	private String getStackTrace(Exception e)
	{
		StackTraceElement[] trace = e.getStackTrace();
		StringBuilder sb = new StringBuilder(e.getMessage())
									.append("\n");
		for (StackTraceElement t : trace)
		{
			sb.append(t)
				.append("\n");
		}
		
		return sb.toString();
	}
	
	/**
	 * 取得方法参数内容
	 * 
	 * @param point 连接点
	 * @return 参数内容
	 */
	private String getContent(JoinPoint point)
	{
		StringBuilder sb = new StringBuilder();
		for (Object object : point.getArgs())
		{
			sb.append(ReflectionUtil.serialize(object));
		}
		return sb.toString();
	}
}
